package com.home.webapp.base.service.impl;

import com.home.base.core.framework.msg.PageResponse;
import com.home.security.core.encryption.HashUtils;
import com.home.webapp.common.constants.user.UserStatusConstant;
import com.home.webapp.common.constants.user.UserTypeConstant;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.utils.query.QueryParams;
import com.home.common.utils.string.StringUtils;
import com.home.webapp.base.entity.BaseUser;
import com.home.webapp.base.mapper.BaseUserMapper;
import com.home.webapp.base.service.BaseUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description: 用户信息 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseUserServiceImpl extends BaseServiceImpl<BaseUserMapper, BaseUser, String> implements BaseUserService {
    private static final Logger logger = LoggerFactory.getLogger(BaseUserServiceImpl.class);

    /***
     * 功能说明：分页
     *
     * @author 王大宸
     * @date 2020/10/6 23:32
     * @param params limit/page/name
     * @return com.home.common.framework.msg.PageResponse
     */
    @Override
    public PageResponse<BaseUser> page(Map<String, Object> params) {
        QueryParams queryParams = new QueryParams(params);
        Page<BaseUser> page = PageHelper.startPage(queryParams.getPage(), queryParams.getLimit());
        List<BaseUser> list = baseMapper.page(queryParams);
        return PageResponse.ok(page.getTotal(), list);
    }

    /***
     * 功能说明：新增用户
     *
     * @author 王大宸
     * @date 2020/10/6 1:01
     * @param entity entity
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseUser>
     */
    @Override
    public BaseUser saveInfo(BaseUser entity) throws BizException {
        if (StringUtils.isEmpty(entity.getUsername())) {
            throw new BizException("登录账号不能为空!");
        }
        if (StringUtils.isEmpty(entity.getPassword())) {
            throw new BizException("密码不能为空!");
        }
        BaseUser one = baseMapper.getByUsername(entity.getUsername());
        if (null != one) {
            throw new BizException("登录账号 " + entity.getUsername() + " 已经存在!");
        }
        // 获取密码盐
        String salt = HashUtils.getSalt();
        entity.setSalt(salt);
        entity.setStatus(UserStatusConstant.IS_OK);
        entity.setType(UserTypeConstant.USER_TYPE);
        // 生成密码
        entity.setPassword(HashUtils.toHash256(entity.getPassword(), salt));
        return super.saveInfo(entity);
    }

    /***
     * 功能说明：修改用户
     *
     * @author 王大宸
     * @date 2020/10/6 1:01
     * @param entity entity
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseUser>
     */
    @Override
    public BaseUser updateInfo(BaseUser entity) throws BizException {
        if (UserStatusConstant.LOCKED_ACCOUNT.equals(entity.getStatus())) {
            BaseUser user = baseMapper.selectByPrimaryKey(entity.getId());
            if (UserTypeConstant.SUPER_ADMIN.equals(user.getUsername())) {
                throw new BizException(UserTypeConstant.SUPER_ADMIN + " 账号不能被冻结");
            }
        }
        return super.updateInfo(entity);
    }

    /***
     * 功能说明：删除用户
     *
     * @author 王大宸
     * @date 2020/10/17 23:57
     * @param id
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BaseUser>
     */
    @Override
    public int remove(String id) throws BizException {
        BaseUser user = baseMapper.selectByPrimaryKey(id);
        if (null == user) {
            return super.remove(id);
        }
        if (UserTypeConstant.SUPER_ADMIN.equals(user.getUsername())
                || UserTypeConstant.SUPER_ADMIN_TYPE.equals(user.getType())
                || UserTypeConstant.ADMIN_TYPE.equals(user.getType())) {
            throw new BizException("管理员账号不能被删除");
        }
        return super.remove(id);
    }
}
